#include "kernel/types.h"
#include "user/user.h"

void primes(int pipeRead){
	int prime;
	int otherNum;
	int p[2];
	pipe(p);
	if(read(pipeRead, &prime, sizeof(int)) != 0){
		fprintf(1, "prime %d\n", prime);
	}else{
		close(p[0]);
		close(p[1]);
		close(pipeRead);
		exit(0);
	}
	int pid = fork();
	if(pid == 0){
		close(pipeRead);
		close(p[1]);
		primes(p[0]);
	}
	close(p[0]);
	while(read(pipeRead, &otherNum, sizeof(int)) != 0){
		if(otherNum % prime != 0){
			write(p[1], &otherNum, sizeof(int));
		}
	}
	close(p[1]);
	close(pipeRead);
	wait(&pid);
	exit(0);

}
int main(){
	int p[2];
	pipe(p);
	for(int i = 2; i<=35; ++i){
		write(p[1], &i, sizeof(int));
	}
	close(p[1]);
	primes(p[0]);
	exit(0);
}
